<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Messenger/classes/PhpFox_ComponentCrush.class.php, $Author:  $ */



/** Crush component
 *
 * @package module.messenger
 * @version $Id: PhpFox_ComponentCrush.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *   PHPFox Team ()
 */
class PhpFox_Mod_Messenger_ComponentCrush extends BaseComponent
{
    function process()
    {
        $oReq = &App::getRequest();
        $oSrvMessenger = $this->_oModule->getService('Messenger');
        $oSrvAccount = &App::getModuleService('Account', 'Security');
        $oSrvAccount->checkAccess('crush');
        $aCond = array();
        $iPage = $oReq->get('page', 1);
        $iPageSize = App::getParam('public.pagesize');

        switch ($oReq->getAction())
        {
            case 'DelCrush':
                $aData = $oReq->get('id');
                if(!empty($aData))
                {
                    $oUrl = App::getUrl();
                	$oSrvMessenger->delItems($aData);
                	App::forward($oUrl->getUrl(), App::format('Mod_Messenger.crush.deleted'), 'message', App::getPageName(), 'Crush');
                }
				break;
        }
        switch ($oReq->get('box'))
        {
            case 'sentbox':
            	$sBoxName = App::format('crush_sentbox');
            	$aCond = array('from' => ' = "'.$oSrvAccount->getCurrentUserLogin().'"', 'type'=> '="crush"');
            	$sDesc = App::format('Mod_Messenger.crush_info_sentbox');
            	break;
           	default :
                $sBoxName = App::format('crush_inbox');
                $aCond = array('to' => ' = "'.$oSrvAccount->getCurrentUserLogin().'"', 'type'=> '="crush"');
                $sDesc = App::format('Mod_Messenger.crush_info_inbox');
        		break;
        }

        $sOrder = 'time DESC';

        list($aMessenger, $iCnt) = $oSrvMessenger->listMessages($aCond, $iPage, $iPageSize, $sOrder);
        $aConf = App::getParam('seen');
        foreach($aMessenger as $i=>$aItem)
        {
	        if(intval($aItem['seen']) === $aConf['viewed'])
				$aMessenger[$i]['seen'] = $sBoxName == App::format('Mod_Messenger.crush_inbox') ? App::format('Mod_Messenger.not_guessed') : App::format('Mod_Messenger.viewed');
			if(intval($aItem['seen']) === $aConf['guess'])
				$aMessenger[$i]['seen'] = App::format('Mod_Messenger.guessed');
			if(intval($aItem['seen']) === intval($aConf['new']))
				$aMessenger[$i]['seen'] = ($oReq->get('box') != 'sentbox' ? App::format('Mod_Messenger.new') : App::format('Mod_Messenger.unseen'));
        }

        $oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);

        $oTpl = &$this->_oModule->getTemplate();

        $oTpl->assign(array(
                             'aMessenger'   => $aMessenger,
        				     'aErrors'		=> $this->getErrors(),
                             'aPaging'      => $oPager->getInfo(App::getUrl()),
       						 'sBoxName'		=> $oReq->get('box'),
        					 'sDescription' => $sDesc,
                             'sUrlView'     => $this->getParam('url_view'),
        					 'sCrushHelp'	=> App::format('Mod_Messenger.crush.help'),
        ));

        $sHtml = $oTpl->fetch('Crush.html');

        return $sHtml;
    }
}
?>